﻿Public Class frmGenerateReport
    Dim dbReport As New dbReport
    Dim dbMaster As New dbMasterTable
    Private type As String
    Private dsReport As DataSet

    Public Sub Initialize(title As String, reportType As String)
        lblTitle.Text = title
        type = reportType
    End Sub

    Private Sub tsExit_Click(sender As Object, e As EventArgs) Handles tsExit.Click
        Me.Close()
    End Sub

    Private Sub frmGenerateReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Select Case type
            Case "Journal"
                gbJournal.Visible = True
                Dim dtJournalYear As DataTable = dbMaster.GetJournalYearList()
                If (dtJournalYear.Rows.Count > 0) Then
                    For i = 0 To dtJournalYear.Rows.Count - 1
                        Try
                            cmbRptYear.Items.Add(dtJournalYear.Rows(i)(0).ToString().Trim())
                        Catch ex As Exception
                        End Try
                    Next
                    cmbRptYear.Items.Add("")
                End If
                generatePeriod()
                Dim dtJournalCode As DataTable = dbMaster.GetJournalCodeList()
                If (dtJournalCode.Rows.Count > 0) Then
                    For i = 0 To dtJournalCode.Rows.Count - 1
                        Try
                            cmbJournalCode.Items.Add(dtJournalCode.Rows(i)(0).ToString().Trim() + " - " + dtJournalCode.Rows(i)(1).ToString().Trim())
                        Catch ex As Exception
                        End Try
                    Next
                End If
        End Select
    End Sub

    Private Sub generatePeriod()
        Select Case type
            Case "Journal"
                cmbRptPeriod.Items.Clear()
                Dim dtJournalPeriod As DataTable = dbMaster.GetJournalPeriodList(cmbRptYear.Text)
                If (dtJournalPeriod.Rows.Count > 0) Then
                    For i = 0 To dtJournalPeriod.Rows.Count - 1
                        Try
                            cmbRptPeriod.Items.Add(dtJournalPeriod.Rows(i)(0).ToString().Trim())
                        Catch ex As Exception
                        End Try
                    Next
                    cmbRptPeriod.Items.Add("")
                End If
        End Select
    End Sub

    Private Sub tsPrint_Click(sender As Object, e As EventArgs) Handles tsPrint.Click
        Select Case type
            Case "Journal"
                Dim journalCode As Array = cmbJournalCode.Text.Split("-")
                dsReport = dbReport.GenerateJournalReport(journalCode(0).ToString().Trim(), cmbRptYear.Text, cmbRptPeriod.Text, txtJournalNoStart.Text, txtJournalNoEnd.Text)
        End Select
        If (dsReport IsNot Nothing) Then
            Dim frmViewReport As New ViewReport
            frmViewReport.strReport = type
            frmViewReport.dsReport = dsReport
            frmViewReport.Show()
            Me.Close()
        End If
    End Sub

    Private Sub CheckNumeric(sender As Object, e As EventArgs) Handles txtJournalNoStart.TextChanged, txtJournalNoEnd.TextChanged
        Dim txt As TextBox = sender
        If (txt.Text <> "" And IsNumeric(txt.Text) = False) Then
            txt.Text = ""
        End If
    End Sub

    Private Sub cmbRptYear_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbRptYear.SelectedIndexChanged
        generatePeriod()
    End Sub
End Class